package com.eduction.common;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class DocumentHandler {

	private Configuration configuration = null;

	public DocumentHandler() {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
	}

	/**
	 * 模板一(专业定位与规划)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws IOException
	 */
	public void createDoc1(HttpServletRequest request, HttpServletResponse res, Map<String, Object> dataMap,
			String fileName) throws IOException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/template");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("Expert.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		if (!outFile.exists()) {
			outFile.createNewFile();
		}
		try {
			FileOutputStream fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			Writer out = new BufferedWriter(oWriter);
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		res.setHeader("content-type", "application/octet-stream");
		res.setContentType("application/octet-stream");
		res.setHeader("Content-Disposition", "attachment;filename=" + fileName1);
		byte[] buff = new byte[1024];
		BufferedInputStream bis = null;
		OutputStream os = null;
		try {
			os = res.getOutputStream();
			bis = new BufferedInputStream(new FileInputStream(outFile));
			int i = bis.read(buff);
			while (i != -1) {
				os.write(buff, 0, buff.length);
				os.flush();
				i = bis.read(buff);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 模板二(课程建设与实施)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc2(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("CurriculumConstructionAndImplementation.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板三(教育合作与实践)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc3(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("EducationalCooperationAndPractice.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板四(教师素质与发展)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc4(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("TeacherQualityAndDevelopment.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板五(办学条件与经费)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc5(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("SchoolConditionsAndFunds.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板六(学生素质与发展)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc6(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("QualityAndDevelopmentOfStudents.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 模板七(汇总表)
	 * 
	 * @param dataMap
	 * @param fileName
	 * @throws UnsupportedEncodingException
	 */
	public void createDoc7(HttpServletRequest request, HttpServletResponse response, Map<String, Object> dataMap,
			String fileName) throws UnsupportedEncodingException {
		// dataMap 要填入模本的数据文件
		// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		// 这里我们的模板是放在template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/orange/file");
		Template t = null;
		try {
			// test.ftl为要装载的模板

			t = configuration.getTemplate("SummarySheet.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 输出文档路径及名称
		File outFile = new File(fileName);
		Writer out = null;
		FileOutputStream fos = null;
		String fileName1 = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());

		fileName1 = java.net.URLEncoder.encode(fileName1, "UTF-8");
		response.setHeader("Content-Type", "application/msexcel");
		response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName1));
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			// 这个地方对流的编码不可或缺，使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。
			// out = new BufferedWriter(new OutputStreamWriter(new
			// FileOutputStream(outFile)));
			out = new BufferedWriter(oWriter);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			t.process(dataMap, out);
			out.close();
			fos.close();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
